﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Client.ViewModel;

namespace Client.View
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();
            InitLanguageDictionary();
        }

        private void SelectQuizUserControl_OnOnStartClicked(int quizid)
        {
            var quizViewModel = new QuizViewModel(quizid);
            QuizUserControl.ViewModel = quizViewModel;

            SelectQuizUserControl.Visibility = Visibility.Collapsed;
            QuizUserControl.Visibility = Visibility.Visible;
        }

        private void QuizUserControl_OnFinishClicked(int totalscore, int score,int quizId)
        {
            var resultViewModel = new ResultViewModel(totalscore, score, quizId);
            ResultUserControl.DataContext = resultViewModel;

            QuizUserControl.Visibility = Visibility.Collapsed;
            ResultUserControl.Visibility = Visibility.Visible;
        }

        private void NewQuiz_OnClick(object sender, RoutedEventArgs e)
        {
            if(SelectQuizUserControl.DataContext != null)
            {
                if (MessageBox.Show("Ha folytatod, elveszik az aktuális kitöltés. Biztos vagy benne?","Confirmation", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                    return;
            }

            var selectQuizViewModel = new SelectQuizViewModel();
            SelectQuizUserControl.DataContext = selectQuizViewModel;

            QuizUserControl.Visibility = Visibility.Collapsed;
            ResultUserControl.Visibility = Visibility.Collapsed;
            HighscoreUserControl.Visibility = Visibility.Collapsed;
            SelectQuizUserControl.Visibility = Visibility.Visible;
        }

        private void ResultUserControl_OnOnSaveClicked()
        {
            (ResultUserControl.DataContext as ResultViewModel).Save();
            Showhighscores();
        }

        private void Showhighscores()
        {
            var highScoreViewModel = new HighscoreViewModel();
            HighscoreUserControl.DataContext = highScoreViewModel;

            HighscoreUserControl.Visibility = Visibility.Visible;
            QuizUserControl.Visibility = Visibility.Collapsed;
            ResultUserControl.Visibility = Visibility.Collapsed;
            SelectQuizUserControl.Visibility = Visibility.Collapsed;
        }

        private void HightscoreClick(object sender, RoutedEventArgs e)
        {
            Showhighscores();
        }

        private void InitLanguageDictionary()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var languagesetting = config.AppSettings.Settings["language"].Value;
            var dict = new ResourceDictionary();
            switch(languagesetting)
            {
                case "default":
                    switch (Thread.CurrentThread.CurrentCulture.ToString())
                    {
                        case "en-US":
                            dict.Source = new Uri("..\\Resources\\StringResources.en-US.xaml",UriKind.Relative);
                            break;
                        case "hu-HU":
                            dict.Source = new Uri("..\\Resources\\StringResources.hu-HU.xaml",UriKind.Relative);
                            break;
                        default:
                            dict.Source = new Uri("..\\Resources\\StringResources.hu-HU.xaml",UriKind.Relative);
                            break;
                    }
                    break;
                case "hungarian":
                    dict.Source = new Uri("..\\Resources\\StringResources.hu-HU.xaml",UriKind.Relative);
                    break;
                case "english":
                    dict.Source = new Uri("..\\Resources\\StringResources.en-US.xaml",UriKind.Relative);
                    break;
            }
            this.Resources.MergedDictionaries.Add(dict);
        }

        private void LanguageSelected(object sender, RoutedEventArgs e)
        {
            var actualDictionary = this.Resources.MergedDictionaries.FirstOrDefault();
            if(actualDictionary != null)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                switch (((MenuItem)sender).Tag.ToString())
                {
                    case "English":
                        actualDictionary.Source = new Uri("..\\Resources\\StringResources.en-US.xaml",UriKind.Relative);
                        config.AppSettings.Settings["language"].Value = "english";
                        break;
                    case "Hungarian":
                        actualDictionary.Source = new Uri("..\\Resources\\StringResources.hu-HU.xaml", UriKind.Relative);
                        config.AppSettings.Settings["language"].Value = "hungarian";
                        break;
                    case "Default":
                        switch (Thread.CurrentThread.CurrentCulture.ToString())
                        {
                            case "en-US":
                                actualDictionary.Source = new Uri("..\\Resources\\StringResources.en-US.xaml",UriKind.Relative);
                                break;
                            case "hu-HU":
                                actualDictionary.Source = new Uri("..\\Resources\\StringResources.hu-HU.xaml",UriKind.Relative);
                                break;
                            default:
                                actualDictionary.Source = new Uri("..\\Resources\\StringResources.hu-HU.xaml",UriKind.Relative);
                                break;
                        }
                        config.AppSettings.Settings["language"].Value = "default";
                        break;
                }
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
        }
    }
}
